using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Entity;
using Utilities;


namespace DAL
{
    public class OutBoxDataAccess : FreshCmsAccessBase
    {
        #region code CMS
        public List<OutBox> GetOutBoxList()
        {
            try
            {
                const string spName = "spo_OutBox_GetList";

                var OutBoxList = DbHelper.ExecuteSPList<OutBox>(spName);
                return OutBoxList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return new List<OutBox>();
            }
        }

        public List<OutBox> GetOutBoxListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        {
            try
            {
                const string spName = "spo_OutBox_GetByPaging";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PageIndex", pageIndex),
										new SqlParameter("@PageSize", pageSize),
										new SqlParameter("@TotalRecord", totalRecord)
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
                var OutBoxList = DbHelper.ExecuteSqlList<OutBox>(sqlCommand);
				if (sqlCommand.Parameters["@TotalRecord"].Value != null)
                {
                    totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
                }

                return OutBoxList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return new List<OutBox>();
            }
        }

        public OutBox GetOutBoxById(int boxId)
        {
            try
            {
                const string spName = "spo_OutBox_GetById";
                var sqlParams = new[]
                                    {
										new SqlParameter("@BoxId", boxId)
                                    };

                var OutBox = DbHelper.ExecuteSPList<OutBox>(spName, sqlParams).SingleOrDefault();
                return OutBox;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return null;
            }
        }

        public int InsertOutBox(OutBox OutBox)
        {
            try
            {
                const string spName = "spo_OutBox_Insert";
                var sqlParams = new[]
                                    {

										new SqlParameter("@ItemId", OutBox.ItemId),
										new SqlParameter("@BoxOrder", OutBox.BoxOrder),
										new SqlParameter("@BoxCategory", OutBox.BoxCategory),
										new SqlParameter("@BoxImage", OutBox.BoxImage),
										new SqlParameter("@BoxType", OutBox.BoxType),
										new SqlParameter("@BoxId", OutBox.BoxId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@BoxId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@BoxId"].Value != null)
                {
                    return (int) sqlCommand.Parameters["@BoxId"].Value;
                }

				return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int UpdateOutBox(OutBox OutBox)
        {
            try
            {
                const string spName = "spo_OutBox_Update";
                var sqlParams = new[]
                                    {
										new SqlParameter("@BoxId", OutBox.BoxId)
										, new SqlParameter("@ItemId", OutBox.ItemId)
										, new SqlParameter("@BoxOrder", OutBox.BoxOrder)
										, new SqlParameter("@BoxCategory", OutBox.BoxCategory)
										, new SqlParameter("@BoxImage", OutBox.BoxImage)
										, new SqlParameter("@BoxType", OutBox.BoxType)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int DeleteOutBox(int boxId)
        {
            try
            {
                const string spName = "spo_OutBox_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@BoxId", boxId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error, exp.ToString());

                return -1;
            }
        }
		#endregion
    }
}
